﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using JTextSdd;
using JtextEpicsInterface;
using jtext_EpicsServer;
using System.Collections;

//this is a JEServer demo that usa the PCCounter Driver
namespace Jetest2
{
    class Program
    {
        static void Main(string[] args)
        {
            #region old
            /*
            string TDRVPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "drv\\";
            JEpicsServer MyJEServer = new JEpicsServer("TEST:PCCOUNT", -1); //create the JEServer
            MyJEServer.LoadDrivers(TDRVPath);
            Record_prop TRecorProp = new Record_prop();
            //RecordDriverProp TDrvPro = new RecordDriverProp();

            JEDevice MyDevice = new JEDevice("TEST4", "notype", true, null);
            JEDevice MyDevice2 = new JEDevice("TEST44", "notype", true, null);

            //record prop for cpu usage
            TRecorProp.HIGH = 50;
            TRecorProp.HIHI = 70;
            //the driver property
            TRecorProp.Drv_name = "JEDPCounter";
            TRecorProp.pvName = "TEST:CPU2";
            TRecorProp.Drv_path = "cpu";
            TRecorProp.DataClass = 0;
            TRecorProp.isInput = true;
            TRecorProp.SCANINTERVAL = 2000;
            MyJEServer.AddRecord<double>(TRecorProp);
            MyDevice.addDeviceProperty("cpu", TRecorProp.pvName, "double;input");

            ////record prop for ram usage
            TRecorProp = new Record_prop();
            //TRecorProp = new RecordDriverProp();
            TRecorProp.LOLO = 300;
            TRecorProp.LOW = 500;
            TRecorProp.HIGH = 5000;
            TRecorProp.HIHI = 7000;
            //the driver property
            TRecorProp.Drv_name = "JEDPCounter";
            TRecorProp.Drv_path = "ram";
            TRecorProp.pvName = "TEST:RAM2";
            TRecorProp.isInput = true;
            TRecorProp.SCANINTERVAL = 4000;
            TRecorProp.DataClass = 0;
            MyJEServer.AddRecord<double>(TRecorProp);
            MyDevice.addDeviceProperty("ram", TRecorProp.pvName, "double;input");

            ////record prop for ram usage
            TRecorProp = new Record_prop();
            //TRecorProp = new RecordDriverProp();
            TRecorProp.LOLO = 300;
            TRecorProp.LOW = 500;
            TRecorProp.HIGH = 5000;
            TRecorProp.HIHI = 7000;
            //the driver property
            TRecorProp.Drv_name = "nothing";
            TRecorProp.Drv_path = "nothing";
            TRecorProp.pvName = "TEST:Output2";
            TRecorProp.isInput = false;
            TRecorProp.SCANINTERVAL = 4000;
            TRecorProp.DataClass = 0;
            MyJEServer.AddRecord<double>(TRecorProp);

            MyDevice.addDeviceProperty("OutTest", TRecorProp.pvName, CommonConst.DT_DOUBLE + ";" + CommonConst.DR_OUTPUT);



            //array test
            TRecorProp = new Record_prop();
            TRecorProp.Drv_name = "nothing";
            TRecorProp.Drv_path = "nothing";
            TRecorProp.pvName = "TEST:ARRAY";
            TRecorProp.isInput = true;
            TRecorProp.DataClass = 1;
            TRecorProp.ArrayLen = 10;
            MyJEServer.AddRecord<sbyte>(TRecorProp);
            MyDevice2.addDeviceProperty("array", TRecorProp.pvName, "SByteArray;Output");

            MyJEServer.StartServer();
            MyDevice2.PublishDevice();
            MyDevice.PublishDevice();
            */
            #endregion

            #region JEServer With sdd
            //this is a general perpose JEserver With sdd suport
            string WorkingPath = AppDomain.CurrentDomain.BaseDirectory.ToString() ;
            //create sdd object
            SDDtoEpics mySdd = new SDDtoEpics(WorkingPath + "sdd.xml");
            Console.WriteLine("This is a general perpose JEserver With sdd suport,and with device support.");
            Console.WriteLine("2010-06-03");
            JEpicsServer MyJEServer = new JEpicsServer(mySdd.IocName,(int)(mySdd.IocInterval)); //create the JEServer
            
            MyJEServer.LoadDrivers(WorkingPath +"drivers//");
            MyJEServer.LoadPlugIns(WorkingPath + "plugins//");
            #region Ini_device
            Hashtable Table_Device = new Hashtable();
            ArrayList[] Devicelist = mySdd.DevicesRegister();
            foreach (ArrayList dev in Devicelist)
            {   //put device in to device list 
                JEDevice myDev = new JEDevice((string)dev[0], (string)dev[1], true, MyJEServer);
                Table_Device.Add((string)dev[0], myDev);
                Console.WriteLine("Device added:" + (string)dev[0]);
                try
                {
                    //load device support
                    myDev.LoadDeviceSupport(WorkingPath + "DeviceSupport//" + myDev.DeviceType + ".dll");
                }
                catch
                {
                    Console.WriteLine("No Device Support for " + myDev.DeviceType);
                }
            }

            #endregion

            Record_prop[] TRecords;
            TRecords = mySdd.getAllRecord();
            foreach (Record_prop TRecord in TRecords)
            {
                Console.WriteLine("Add PV " + TRecord.pvName );   
                MyJEServer.AddRecord(TRecord);  //add record 
                //added to device if needed
                if (Table_Device.Contains(TRecord.DeviceName))
                {
                    ((JEDevice)Table_Device[TRecord.DeviceName]).addDeviceProperty(TRecord.DeviceProperty, TRecord.pvName, TRecord.DeviceDtatType);
                }
            }

            //start the device
            foreach (DictionaryEntry dev in Table_Device)
            {
                ((JEDevice)(dev.Value)).PublishDevice();
            }

            MyJEServer.StartServer();

            Console.WriteLine("Server Has Started");   
            #endregion


            while (true)
            {
                
                Thread.Sleep(1000);
                //MyJEServer.BenchFlush();
            }

            
        }
    }
}
